package com.lantosec.hmrp.modules.archives.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lantosec.hmrp.core.shiro.TokenUtil;
import com.lantosec.hmrp.core.util.DateUtils;
import com.lantosec.hmrp.core.util.UUIDUtils;
import com.lantosec.hmrp.modules.archives.entity.UserWork;
import com.lantosec.hmrp.modules.archives.mapper.UserWorkMapper;
import com.lantosec.hmrp.modules.archives.service.IUserWorkService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lantosec.hmrp.modules.archives.vo.UserWorkVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.context.annotation.Primary;
import lombok.extern.log4j.Log4j2;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 履历信息表 服务实现类
 * </p>
 *
 * @author AutoCode
 * @since 2020-06-09
 */
@Service
@Primary
@Log4j2
public class UserWorkServiceImpl extends ServiceImpl<UserWorkMapper, UserWork> implements IUserWorkService {

    @Override
    public void saveUserWork(UserWorkVO vo) {
        UserWork userWork = vo.convertToEntity();
        userWork.setUserWorkId(UUIDUtils.get());
        //获取当前用户id和名称
        userWork.setCreateId(TokenUtil.getUserId());
        userWork.setCreateUser(TokenUtil.getName());
        userWork.setCreateTime(DateUtils.nowDate());
        userWork.setVerifyStatus(1);
        this.save(userWork);
    }

    @Override
    @Transactional
    public void updateUserWork(UserWorkVO vo) {
        String userWorkId = vo.getUserWorkId();
        //新增一条数据
        //saveUserWork(vo);
        //修改这条数据的审核状态为2
        if(StringUtils.isNotEmpty(userWorkId)){
            UserWork userWork = vo.convertToEntity();
            userWork.setVerifyStatus(1);
            this.updateById(userWork);
        }else {
            saveUserWork(vo);
        }
    }

    @Override
    public List<UserWork> getUserWork(String userId) {
        List<UserWork> userWorkList = this.list(new QueryWrapper<UserWork>().eq(UserWork.ColName.user_id.toString(),userId).orderByDesc(UserWork.ColName.create_time.toString()));
        return userWorkList;
    }

    @Override
    public List<UserWork> getUserWorkVerify(String userId) {
        List<UserWork> userWorks = this.list(new QueryWrapper<UserWork>().eq(UserWork.ColName.user_id.toString(),userId).in(UserWork.ColName.verify_status.toString(),0,2));
        return userWorks;
    }

    @Override
    public void verifyUserWork(UserWorkVO vo) {
        UserWork userWork = vo.convertToEntity();
        Integer verifyStatus = userWork.getVerifyStatus();
        //如果审核不通过
        if(vo.getState() == 0){
            //审核状态为审核中的变成 已审核
            if(verifyStatus == 2){
                userWork.setVerifyStatus(1);
            }
            //审核状态为未审核的变成 审核不通过
            if(verifyStatus == 0){
                userWork.setVerifyStatus(3);
            }
        }
        //如果审核通过
        if(vo.getState() == 1){
            //审核状态为审核中的变为 历史记录
            if(verifyStatus == 2){
                userWork.setVerifyStatus(4);
            }
            //审核状态为未审核的变为 已审核
            if(verifyStatus == 0){
                userWork.setVerifyStatus(1);
            }
        }
        //获取当前用户id和名称
        userWork.setVerifyId(TokenUtil.getUserId());
        userWork.setVerifyUser(TokenUtil.getName());
        userWork.setVerifyTime(DateUtils.nowDate());
        this.updateById(userWork);
    }

    @Override
    public List<UserWork> getUserWorkHistory(String userId) {
        List<UserWork> userWorks = this.list(new QueryWrapper<UserWork>().eq(UserWork.ColName.user_id.toString(),userId).eq(UserWork.ColName.verify_status.toString(),4)
                                                                        .orderByDesc(UserWork.ColName.create_time.toString()));
        return userWorks;
    }
}
